package com.yxinsur.imc.web.controller.api;

import com.yxinsur.imc.business.dto.*;
import com.yxinsur.imc.business.service.IImcOrderService;
import com.yxinsur.imc.common.annotation.ApiLog;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.common.utils.SecurityUtils;
import com.yxinsur.imc.common.validator.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 保单接口服务
 * @author imc
 * @since 2021-07-12
 */
@RestController
@RequestMapping("/api/policy")
@Api(value = "API模块",tags = "API模块")
public class PolicyController {

    private static final Logger logger = LoggerFactory.getLogger(PolicyController.class);
    @Autowired
    private IImcOrderService imcOrderService;
    /**
     * API保单录入
     */
    @ResponseBody
    @PostMapping("/accept")
    @ApiLog(title = "API录单", businessType = BusinessType.INSERT)
    @ApiOperation("保单承保录入接口")
    public AjaxResult acceptOrder(@Validated @RequestBody OrderDto order, BindingResult bindingResult) {
        logger.info("...API请求承保录入");
        if(bindingResult.hasErrors()){
            StringBuffer msg = new StringBuffer();
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                logger.info(fieldError.getDefaultMessage());
                msg.append(fieldError.getDefaultMessage()+";");
            }
            return AjaxResult.error(msg.toString());
        }else{
            this.imcOrderService.receiveOrder(order);
            return AjaxResult.success();
        }
    }


    /**
     * API保单终止, 中止，撤单
     */
    @ResponseBody
    @PostMapping("/termination")
    @ApiLog(title = "API保单终止", businessType = BusinessType.UPDATE)
    @ApiOperation("API保单终止")
    public AjaxResult termination(@Validated @RequestBody OrderEndCancelSuspensionDto order, BindingResult bindingResult) {
        logger.info("...API保单终止");
        if(bindingResult.hasErrors()){
            StringBuffer msg = new StringBuffer();
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                logger.info(fieldError.getDefaultMessage());
                msg.append(fieldError.getDefaultMessage()+";");
            }
            return AjaxResult.error(msg.toString());
        }else{
            String orderStatus = order.getOrderStatus();
            if("40".equals(orderStatus)){
                Assert.isNull(order.getEndCause(),"终止原因[endCause]不能为空");
                this.imcOrderService.ApiEndImcOrder(order);
            }else{
                return AjaxResult.error("保单状态不是终止，请检查！");
            }
            return AjaxResult.success();
        }
    }

    @ResponseBody
    @PostMapping("/suspension")
    @ApiLog(title = "API保单中止", businessType = BusinessType.UPDATE)
    @ApiOperation("API保单中止")
    public AjaxResult suspension(@Validated @RequestBody OrderEndCancelSuspensionDto order, BindingResult bindingResult) {
        logger.info("...API保单中止");
        if(bindingResult.hasErrors()){
            StringBuffer msg = new StringBuffer();
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                logger.info(fieldError.getDefaultMessage());
                msg.append(fieldError.getDefaultMessage()+";");
            }
            return AjaxResult.error(msg.toString());
        }else{
            String orderStatus = order.getOrderStatus();
            if("50".equals(orderStatus)){
                this.imcOrderService.ApiSuspensionImcOrder(order);
            }else{
                return AjaxResult.error("保单状态不是中止，请检查！");
            }
            return AjaxResult.success();
        }
    }


    @ResponseBody
    @PostMapping("/cancel")
    @ApiLog(title = "API保单撤单", businessType = BusinessType.UPDATE)
    @ApiOperation("API保单撤单")
    public AjaxResult cancel(@Validated @RequestBody OrderEndCancelSuspensionDto order, BindingResult bindingResult) {
        logger.info("...API保单撤单");
        if(bindingResult.hasErrors()){
            StringBuffer msg = new StringBuffer();
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                logger.info(fieldError.getDefaultMessage());
                msg.append(fieldError.getDefaultMessage()+";");
            }
            return AjaxResult.error(msg.toString());
        }else{
            String orderStatus = order.getOrderStatus();
            if("30".equals(orderStatus)){
                this.imcOrderService.ApiCancelImcOrder(order);
            }else{
                return AjaxResult.error("保单状态不是撤单，请检查！");
            }
            return AjaxResult.success();
        }
    }


    /**
     * 保单回执,回访
     * @param order
     * @return
     */
    @ResponseBody
    @ApiLog(title = "API保单回执,回访,告知书", businessType = BusinessType.UPDATE)
    @ApiOperation("保单回执,回访,告知书")
    @PostMapping("/receiptRevisitCustom")
    public AjaxResult receiptRevisitCustom(@Validated @RequestBody OrderReceiptRevisitCustomDto order, BindingResult bindingResult){
        logger.info("...API保单回执,回访,告知书");
        if(bindingResult.hasErrors()){
            StringBuffer msg = new StringBuffer();
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                logger.info(fieldError.getDefaultMessage());
                msg.append(fieldError.getDefaultMessage()+";");
            }
            return AjaxResult.error(msg.toString());
        }else{
            this.imcOrderService.apiReceiptRevisitCustom(order);
            return AjaxResult.success();
        }
    }
}
